new Vue({
    delimiters: ['{[', ']}'],
    el: '#app',
    created: function () {
        console.log(config);
        let getFields = function (configure, vkNeeded, returnFirst){
            fields = [];
            for(k in configure){
                for(vk in configure[k]){
                    if(vk == vkNeeded && configure[k][vk] == true ){
                        if(returnFirst){
                            return k;
                        }else{
                            fields.push(k);
                        }
                    }
                }
            }
            if(returnFirst){
                return "";
            }else{
                return fields;
            }
        };
        this.showName = getFields(config, "showName", false);
        this.showInTitle = getFields(config, "showInTitle", false);
        this.showInSubTitle = getFields(config, "showInSubTitle", false);
        this.showInContent = getFields(config, "showInContent", false);
        this.select_options = getFields(config, "isOption", false);
        this.selected_fields = getFields(config, "isSelected", false);
        this.date_field = getFields(config, "isDate", true);
        this.url_field = getFields(config, "isUrl", true);

    },
    data: function() {
        return { 
            initialization: true,
            config: config,
            showName: [],
            showInTitle: [],
            showInSubTitle: [],
            showInContent: [],
            select_options: [],
            selected_fields: [],
            date_field: "",
            url_field: "",
            input: "",
            pagenum: 1,
            pagelen: 5,
            datasets: [],
            is_last_page: false,
            fullscreenLoading: false,
            more_search_loading: false,
            date_start_to_end: [new Date(2019, 10, 10, 10, 10), new Date()],
            pickerOptions: {
                shortcuts: [{
                    text: '最近一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [start, end]);
                    }
                },{
                  text: '最近半年',
                  onClick(picker) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(start.getTime() - 3600 * 1000 * 24 * 180);
                    picker.$emit('pick', [start, end]);
                  }
                },{
                    text: '最近一年',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 365);
                        picker.$emit('pick', [start, end]);
                    }
                },{
                    text: '最近三年',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 365 * 3);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            }
              
        }
    },
    methods:{
        search: function(){
            this.datasets = []
            this.is_last_page = false
            this.pagenum = 1;
            if (this.input.length==0){
                this.initialization = true;
                return;
            }
            this.initialization = false;
            this.fullscreenLoading = true;
            axios.post('/search',{
                fields: this.selected_fields,
                word: this.input,
                date_field: this.date_field,
                date_start: this.date_start_to_end[0],
                date_end: this.date_start_to_end[1],
                pagenum: parseInt(this.pagenum),
                pagelen: parseInt(this.pagelen)
            })
            .then( response => {
                console.log(response);
                this.fullscreenLoading = false;
                this.$message({
                    message: '查询成功!',
                    type: 'success'
                });
                this.datasets = response.data["datasets"];
                this.is_last_page = response.data["is_last_page"];
            })
            .catch( error => { 
                console.log(error);
                this.$message.error(error);
                this.fullscreenLoading = false;
            })
        },
        moreSearch: function(){
            if(this.is_last_page == true || this.datasets.length == 0 ){
                console.log("没有更多数据，不进行加载");
                return null;
            }
            this.more_search_loading = true;
            this.pagenum = this.pagenum + 1;
            axios.post('/search',{
                fields: this.selected_fields,
                word: this.input,
                date_field: this.date_field,
                date_start: this.date_start_to_end[0],
                date_end: this.date_start_to_end[1],
                pagenum: parseInt(this.pagenum),
                pagelen: parseInt(this.pagelen)
            })
            .then( response => {
                console.log(response);
                this.more_search_loading = false;
                this.$message({
                    message: '查询成功!',
                    type: 'success'
                });
                this.datasets = this.datasets.concat(response.data["datasets"]);
                this.is_last_page = response.data["is_last_page"];
            })
            .catch( error => { 
                console.log(error);
                this.$message.error(error);
                this.more_search_loading = false;
            })
        }
    },
    
})